import Vue from 'vue'
import VueRouter from 'vue-router'
// 登录页
import login from '@/views/user/login.vue'

import layout from '@/views/layout.vue'

import home from '@/views/home/home.vue'
import article from '@/views/home/article.vue'
import search from '@/views/home/search.vue'
import searchResult from '@/views/home/searchResult.vue'

import movie from '@/views/movie/movie.vue'
import qa from '@/views/qa/qa.vue'
import user from '@/views/user/user.vue'


import notfound from '@/views/notfound/notfound.vue'

import edit from '@/views/user/edit.vue'
import student from '@/views/user/student.vue'





Vue.use(VueRouter)
// 配置路由规则
const routes = [{
    //登录页
    path: '/login',
    name: 'login',
    component: login,

    //元信息
    // meta: {
    //   Key: value,
    //   key: value2
    // }
  },
  {
    //编辑页
    path: '/edit',
    name: 'edit',
    component: edit,
  },
  {
    // 文章详情页
    path: '/article',
    name: 'myarticle',
    component: article,
  },
  {
    // 文章搜索页
    path: '/search',
    name: 'search',
    component: search,
  },
  {
    // 文章搜索结果页
    path: '/searchResult',
    name: 'searchResult',
    component: searchResult,
  },
  {
    // 小智同学页
    path: '/student',
    name: 'student',
    component: student,
  },
  {
    //layout页
    path: '/',
    name: 'layout',
    component: layout,
    // 二级路由
    children: [{
        //首页
        path: '/home',
        name: 'home',
        component: home
      },
      {
        //视频页
        path: '/movie',
        name: 'movie',
        component: movie
      },
      {
        //问答页
        path: '/qa',
        name: 'qa',
        component: qa
      },
      {
        //编辑页
        path: '/edit',
        name: 'edit',
        component: edit,
      },
      {
        //用户页
        path: '/user',
        name: 'user',
        component: user,
        // 添加登录判断的路由元信息
        meta: {
          isLogin: true
        }
      },
      {
        path: '/',
        redirect: '/home'
      },
    ]
  },
  {
    //notfound404页
    path: '/notfound',
    name: 'notfound',
    component: notfound
  },
  {
    path: '*',
    redirect: '/notfound'
  },



]

const router = new VueRouter({
  routes
})
import {
  getToken
} from '@/utils/token.js'
import {
  Toast
} from 'vant'

router.beforeEach((to, from, next) => {
  // console.log(to);
  // console.log(from);

  if (to.meta.isLogin) {
    if (getToken()) {
      next()
    } else {
      Toast.fail('请登录')
      next('/login')
      // 3.清除浏览器爆红提示
      setTimeout(() => {
        console.clear()
      }, 1000);
    }
  } else {
    next()
  }

})

export default router
